package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.feign.QueryUsersByIdsDTO;
import io.github.talelin.latticy.dto.feign.UpdateOmsUserDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.dto.user.RegisterDTO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.AdminService;
import io.github.talelin.latticy.service.GroupService;
import io.github.talelin.latticy.service.UserService;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @authoer:wulongbo
 * @createDate:2022/5/28
 * @description:
 */
@RestController
@RequestMapping("/user/openfeign")
@Slf4j
public class UserFeignController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private GroupService groupService;

    /**
     * 用户注册Feign
     */
    @PostMapping("/register")
    @AdminRequired
    public UserDO register(@RequestBody @Validated RegisterDTO validator) {
        return userService.createUser(validator);
    }

    @AdminRequired
    @GetMapping("/user/{id}")
    @PermissionMeta(value = "根据用户id查询用户信息", mount = false)
    public UserDO getUserById(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return userService.getUserById(id);
    }

    @AdminRequired
    @GetMapping("/user/log/{id}")
    @PermissionMeta(value = "根据用户id获取OMS用户信息包含删除（用于日志记录）", mount = false)
    public UserDO getOmsUserAfterLogById(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return userService.getUserAfterLogById(id);
    }

    @AdminRequired
    @GetMapping("/getUsersByIds")
    @PermissionMeta(value = "根据用户id查询用户列表", mount = false)
    public PageResponseVO<UserDO> getUsersByIds(@Validated QueryUsersByIdsDTO dto) {
        IPage<UserDO> iPage = adminService.getUserPageByUserIds(dto.getUserIds(), dto.getCount(), dto.getPage());
        return PageUtil.build(iPage);
    }


    @GetMapping("/list")
    @GroupRequired
    @PermissionMeta(value = "查询所有用户", mount = false)
    public List<UserDO> getUserList() {
        return  adminService.getUserList();
    }

    @AdminRequired
    @GetMapping("/users")
    @PermissionMeta(value = "查询所有用户", mount = false)
    public PageResponseVO<UserDO> getUsers(@Validated BasePageDTO dto) {
        IPage<UserDO> iPage = adminService.getUserPageByGroupId(dto.getCount(), dto.getPage());
        return PageUtil.build(iPage);
    }

    @PutMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "修改oms客户", mount = false)
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated UpdateOmsUserDTO validator) {
        userService.updateUser(id, validator);
        return new UpdatedVO();
    }


    @DeleteMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "删除oms客户", mount = false)
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        userService.deleteUser(id);
        return new DeletedVO();
    }

    @AdminRequired
    @GetMapping("/getAdminGroupId")
    @PermissionMeta(value = "获取admin分组id", mount = false)
    public List<Integer> getAdminGroupId() {
        return groupService.getAdminGroupId();
    }

}
